package com.dsmbase.corp.project.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dsmbase.corp.common.DmsUtil;
import com.dsmbase.corp.common.ServiceException;
import com.dsmbase.corp.flow.domain.Flow;
import com.dsmbase.corp.flow.domain.FlowNode;
import com.dsmbase.corp.flow.service.FlowService;
import com.dsmbase.corp.project.domain.ProjectFlow;
import com.dsmbase.corp.project.domain.ProjectFlowNode;
import com.dsmbase.corp.project.domain.ProjectID;
import com.dsmbase.corp.project.domain.WorkLoad;
import com.dsmbase.corp.project.service.ProjectService;
import com.dsmbase.corp.user.domain.User;
import com.dsmbase.corp.user.service.UserService;

@Component
@Controller
public class ProjectController {

	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private FlowService flowService;
	
	@RequestMapping(value = "getAllProjects", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectFlow> getAllProjects() {
		return projectService.getAllProjects();
	}
	
	@RequestMapping(value = "createProject" , method = RequestMethod.POST)
	public void createProject(ProjectForm form) {
		
			try {
				projectService.createProject(form);
			} catch (ServiceException e) {
				throw new RuntimeException(e);
			}
		
	}
	
	@RequestMapping(value = "getAllFlow")
	@ResponseBody
	public List<Flow> getAllFlow() {
		try {
			return projectService.getAllFlow();
		} catch (ServiceException e) {
			return null;
		}
	}
	
	@RequestMapping(value = "loadTemplateFlow", method = RequestMethod.POST)
	@ResponseBody
	public ProjectForm loadTemplateFlow(@RequestBody ProjectForm paramForm) {
		ProjectForm form = new ProjectForm();
		try {
			form.setFlowId(paramForm.getFlowId());
			form.setName(paramForm.getName());
			form.setProjectId(paramForm.getProjectId());
			form.setVersion(paramForm.getVersion());
			form.setStartTime(paramForm.getStartTime());
			form.setEndTime(paramForm.getEndTime());
		} catch (Exception e1) {
			// TODO log
			System.out.println(e1.getStackTrace());
		}
		List<ProjectNodeForm> nodeList = new ArrayList<ProjectNodeForm>();
		try {
			List<FlowNode> flowNodes = projectService.getTemplateNodes(paramForm.getFlowId());
			for (FlowNode node : flowNodes) {
				ProjectNodeForm nodeForm = new ProjectNodeForm();
				nodeForm.setId(node.getId());
				nodeForm.setNodeName(node.getName());
				nodeForm.setPlanWorkLoad(node.getWorkLoad());
				nodeList.add(nodeForm);
			}
			form.setNodeList(nodeList);
		} catch (Exception e) {
			// TODO log
			nodeList = null;
		}
		return form;
	}
	
	@RequestMapping(value = "getAllUser")
	@ResponseBody
	public List<User> getAllUser() {
		try {
			List<User> users = userService.getAllUser();
			return users;
		} catch (ServiceException e) {
			return null;
		}
	}
	
	@RequestMapping(value = "getProjectList")
	@ResponseBody
	public List<ProjectID> getAllProjectID() {
		return projectService.getProjectList();
	}
	
	@RequestMapping(value = "newProject")
	public ModelAndView newProject() {
		return new ModelAndView("new");
	}
	
	@RequestMapping(value = "editProject")
	public ModelAndView editProject(int id) {
		try {
			ProjectForm form = projectService.getProjectFormById(id);
			return new ModelAndView("edit", "project", form);
		} catch (ServiceException e) {
			return new ModelAndView("edit");
		}
	}
	
	@RequestMapping(value = "editProjectNode")
	public ModelAndView editProjectNode(int id) {
		try {
			
			ProjectFlowNode node = projectService.getProjectFlowNode(id);
			ProjectFlow projectFlow = projectService.getProjectFlowById(node.getProjectId());
			ProjectForm form = new ProjectForm();
			form.setProjectId(projectFlow.getProjectId());
			form.setId(projectFlow.getId());
			form.setFlowId(projectFlow.getFlowId());
			form.setName(projectFlow.getName());
			form.setProjectId(projectFlow.getProjectId());
			form.setVersion(projectFlow.getVersion());
			form.setStartTime(projectFlow.getStartTime());
			form.setEndTime(projectFlow.getEndTime());
			form.setComment(projectFlow.getComment());
			List<ProjectNodeForm> nodeList = new ArrayList<ProjectNodeForm>();
			ProjectNodeForm nodeForm = new ProjectNodeForm();
			nodeForm.setNodeName(flowService.getFlowNodeById(node.getNodeId()).getName());
			nodeForm.setUserId(node.getUserId());
			nodeForm.setPlanWorkLoad(node.getPlanWorkLoad());
			nodeForm.setActWorkLoad(node.getActWorkLoad());
			nodeForm.setEndTime(node.getEndTime());
			nodeForm.setStartTime(node.getStartTime());
			nodeForm.setStatus(node.getStatus());
			nodeForm.setId(node.getId());
			
			Map<Integer, Integer> map = new HashMap<>();
			List<WorkLoad> workLoads = projectService.getWorkLoadByProjectNodeId(node.getId());
			for(WorkLoad load: workLoads) {
				map.put(load.getMonth(), load.getWorkLoad());
			}
			nodeForm.setWorkLoadRecord(map);
			nodeList.add(nodeForm);
			form.setNodeList(nodeList);
			
			return new ModelAndView("editProjectNode", "project", form);
		} catch (ServiceException e) {
			return new ModelAndView("editProjectNode");
		}
	}
	
	
	@RequestMapping(value = "updateProject", method = RequestMethod.POST)
	public void updateProject(ProjectForm form) {
		try {
			projectService.updateProject(form);
		} catch (ServiceException e) {
			// TODO log
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "getAllProjectNodes")
	@ResponseBody
	public List<ProjectFlowNode> getAllProjectNodes(int userId) {
		try {
			System.out.println("asd");
			List<ProjectFlowNode> list = projectService.getAllProjectNodesByUserId(userId);
			List<ProjectFlowNode> filter = new ArrayList<>();
			for (ProjectFlowNode node : list) {
				if (node.getStatus() == DmsUtil.ProjectStatus.START.ordinal()) {
					filter.add(node);
				}
			}
			return filter;
		} catch (ServiceException e) {
			// TODO log
			return null;
		}
	}
	
	@RequestMapping(value = "updateProjectNode")
	public void updateProjectNode(ProjectForm form) {
		ProjectNodeForm nodeForm = form.getNodeList().get(0);
		int proxyUserId = nodeForm.getProxyUserId();
		System.out.println(proxyUserId);
		User proxyUser = null;
		try {
			proxyUser = userService.getUserBy(proxyUserId);
		} catch (Exception e) {
			// TODO log
			System.out.println(proxyUserId);
		}
		try {
			if (proxyUser != null) {
				projectService.handleProxy(nodeForm, proxyUser);
			} else {
				System.out.println("asddd");
				projectService.updateProjectNode(nodeForm);
			}
		} catch (ServiceException e) {
			// TODO log
		}
		
	}
	
}
